package xin.nick.roc.consumer.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import xin.nick.roc.consumer.proxy.RpcClientProxy;
import xin.nick.rpc.api.IUserService;
import xin.nick.rpc.pojo.User;

/**
 * @author Nick
 * @date 2021/7/4
 * @description
 */
@Component
public class RpcClientStarter implements CommandLineRunner {

    @Autowired
    Environment environment;

    public static void main(String[] args) {
        IUserService userService = (IUserService)RpcClientProxy.createProxy(IUserService.class);
        User user = userService.getById(1);
        System.out.println(user);
        user = userService.getById(3);
        System.out.println(user);
        user = userService.getById(2);
        System.out.println(user);
    }

    @Override
    public void run(String... args) throws Exception {
        // 准备读取yml,拿到provider列表信息
        String providers = environment.getProperty("rpc.providers");
        String[] providerStringArray = providers.split(";");
        for(String providerString : providerStringArray) {
            String[] providerInfoArray = providerString.split(":");
            String ip = providerInfoArray[0];
            String port = providerInfoArray[1];
            ProviderInfo providerInfo = new ProviderInfo();
            providerInfo.setIp(ip);
            providerInfo.setPort(Integer.valueOf(port));
            providerInfo.getProviderList().add(providerInfo);
        }
    }
}
